﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HtmlAgilityPack;
using System.Diagnostics;
using System.IO;
using System.Drawing;
using System.Net;
using LingDong.HtmlParser;

namespace LingDong.PictureKeywords
{
    public class Baseline
    {
        /// <summary>
        /// Get picutre's keywords from a html
        /// A html may contains serval pictures
        /// </summary>
        /// <param name="url"></param>
        /// <param name="html"></param>
        /// <returns>a list of [image src, keywords]</returns>
        public static List<KeyValuePair<string, string>> GetPictureKeywords(string url, string html)
        {
            HtmlDocument doc = new HtmlDocument();
            doc.LoadHtml(html);
            HtmlNodeCollection imageNodeList = doc.DocumentNode.SelectNodes("//img[@src]");
            
            List<KeyValuePair<string, string>> result = new List<KeyValuePair<string, string>>();
            if (imageNodeList == null)
            {
                return result;
            }

            //string content = HtmlContent.GetHtmlContent(doc);
            string abs = HtmlContent.GetHtmlAbstract(doc);
            foreach (HtmlNode imageNode in imageNodeList)
            {
                KeyValuePair<string, string> keyword;
                if (GetKeyword(imageNode, abs, out keyword))
                {
                    result.Add(keyword);
                }
            }
            return result;
        }

        private static bool GetKeyword(HtmlNode imageNode, string htmlAbs, out KeyValuePair<string, string> pair)
        {
            pair = new KeyValuePair<string, string>();

            // get image info
            string alt = String.Empty;
            if (imageNode.Attributes.Contains("alt"))
                alt = imageNode.Attributes["alt"].Value;
            string src = imageNode.Attributes["src"].Value.Trim();

            if (!DownloadedPictures.IsPictureDownload(src))
                return false;

            // extract keywords
            StringBuilder keywords = new StringBuilder();
            keywords.Append(alt);
            keywords.Append(GetSiblingText(imageNode));
            keywords.Append(htmlAbs);

            string singleLineContent = keywords.ToString().Replace("\r\n", "\t").Replace("\n", "\t");
            pair = new KeyValuePair<string, string>(src, singleLineContent);
            return true;
        }
        
        private static string GetSiblingText(HtmlNode imageNode)
        {
            for (int i = 0; i < 2; i++)
            {
                for (int j = 0; j < Settings.ImageTabSiblingNumber; j++)
                {
                    if (imageNode.NextSibling == null)
                        break;
                    string text = imageNode.NextSibling.InnerText.Trim();
                    if (!String.IsNullOrEmpty(text))
                        return text;
                    imageNode = imageNode.NextSibling;
                }

                // find parent's sibling
                imageNode = imageNode.ParentNode;
            }
            return String.Empty;
        }

    }
}
